<?php
/**
 * Created by PhpStorm.
 * User: wei
 * Date: 2016/4/15
 * Time: 13:38
 */
namespace Api\Controller;

use Think\Controller;
use Api\Model\LoginModel;
use Api\Model\RepairOrderModel;

/**
 *装修分类
 */
class RenovationController extends BaseController
{
    //分类
    public function category(){
        $phone = I('post.phone');
        $data = array();
        $banner = M('banner')->where("type=1 and status=1")->order(array('order' => 'desc'))->limit(5)->select();
        $data['banner'] = $banner;

        //修改当前身份状态
        if(!empty($phone)){
            $where['phone']=$phone;
            $map['identity']=1;
            if(M('user')->where($where)->save($map)===false){
                $data=array(code=>403,msg=>false);
                $this->ajaxReturn($data,'JSON');
            }
        }

        $Classify = M('category')->where("status=1 and type=1 and pid=0")->order(array('order'=>'desc'))->select();
        $count = count($Classify);

        for ($i = 0; $i < $count; $i++) {
            $second = M('category')->where("status=1 and type=1 and pid=" . $Classify[$i]['category_id'])->limit(3)->getField('category_name', true);
            //var_dump($second);exit();
            $Classify[$i]['sonClassify'] = implode(',', $second);
        }

        $data['classify'] = $Classify;
        $data = array(errcode => 200, errmsg => $data);
        $this->ajaxReturn($data, 'JSON');
    }



    public function order(){
        $master_type=I('post.masterType');      //师傅类型
        $user_id=I('post.user_id');      //下单用户id
        $address_id=I('post.address_id');      //下单用户地址
        $master_category=I('post.category');    //装修分类
        $area   =I('post.area');         //面积
        $workTime   =I('post.worktime');    //工作经验
        $work   =I('post.work');    //工作单位
        $scope   =I('post.scope');    //服务范围
        $type   =I('post.type');    //服务类型
        $orderType   =I('post.orderType');    //订单类型   1即时下单 2预约下单
        $orderTime   =I('post.orderTime');    //预约时间

        if(empty($master_type)){
            $data = array(errcode => 1002, errmsg =>'未选择师傅类型');
            $this->ajaxReturn($data, 'JSON');
        }

        if(empty($master_category)){
            $data = array(errcode => 1002, errmsg =>'未选择分类');
            $this->ajaxReturn($data, 'JSON');
        }

        if(empty($area)){
            $data = array(errcode => 1002, errmsg =>'未选择面积');
            $this->ajaxReturn($data, 'JSON');
        }

        if(empty($work)){
            $data = array(errcode => 1002, errmsg =>'未选择就职单位');
            $this->ajaxReturn($data, 'JSON');
        }

        if(empty($workTime)){
            $data = array(errcode => 1002, errmsg =>'未选择工作经验');
            $this->ajaxReturn($data, 'JSON');
        }

        if(empty($scope)){
            $data = array(errcode => 1002, errmsg =>'未选择服务范围');
            $this->ajaxReturn($data, 'JSON');
        }

        if(empty($type)){
            $data = array(errcode => 1002, errmsg =>'未选择服务类型');
            $this->ajaxReturn($data, 'JSON');
        }

        if(empty($orderType)){
            $data = array(errcode => 1002, errmsg =>'未选择订单类型');
            $this->ajaxReturn($data, 'JSON');
        }

        if($orderType==2 && empty($orderTime)){
            $data = array(errcode => 1002, errmsg =>'请填写预约时间');
            $this->ajaxReturn($data, 'JSON');
        }

        $addWhere['id']=$address_id;
        $address=M('address')->where($addWhere)->find();
        if(empty($address)){
            $data = array(errcode => 1003, errmsg =>'地址不存在');
            $this->ajaxReturn($data, 'JSON');
        }

        $data['renovation_order_num_alias']=build_order_no();                            // 生成订单号
        $data['user_id']=$user_id;                            // 下单客户id
        $data['shipping_tel']=$address['phone'];                            // 下单人联系方式
        $data['shipping_name']=$address['address'];                            // 下单地址
        $data['shipping_addr']=$address['contacts'];                            // 下单人名字
        $data['house_id']=$master_category;                            // 房屋类型
        $data['size']=$area;                            // 面积
        $data['work_id']=$work;                            // 就职单位
        $data['order_type']=$orderType;                            // 订单类型
        $data['book_time']=$orderTime;                            // 订单类型
        $data['status']=1;                            // 订单类型

        if($orderId=M('renovation_order')->add($data)){
            //下单成功
            //设置redis的订单key值和把订单存进reorder表
            S(array('type' => 'redis'));
            S();
            $where['work_time_id']=$workTime;
            $where['work_addr']=$work;
            $where['type']=$master_type;
            M('user')->where($where)->select();
        }else{
            //下单失败
        }

    }
}